import React, {Component} from 'react'
import '../../less/index.less'
import {withRouter} from 'react-router-dom'
import {connect} from 'react-redux'
import {FormattedMessage} from 'react-intl'
import SelectStatus from './select-status-cell'
import * as rankingStatus from '../../constant/ranking-status'
import * as assignmentQuizMarkAction from '../../actions/assignment-quiz-Mark'

class AssignmentRankingBox extends Component {
  constructor (props) {
    super(props)
    this.state = {
      assignmentQuizMarkId: -1,
      evaluate: '',
      ranking: 'NoScore'
    }
  }
  componentWillReceiveProps (nextProps) {
    const {assignmentQuizMark} = nextProps
    this.setState({
      assignmentQuizMarkId: assignmentQuizMark.id ? assignmentQuizMark.id : -1,
      evaluate: assignmentQuizMark.evaluate ? assignmentQuizMark.evaluate : '',
      ranking: assignmentQuizMark.ranking ? assignmentQuizMark.ranking : 'NoScore'
    })
  }

  handleSubmit (ranking) {
    const {assignmentId, quizId, studentId} = this.props
    const {evaluate, assignmentQuizMarkId} = this.state
    if (this.isAddOrEditAction(assignmentQuizMarkId)) {
      this.props.addAssignmentQuizMark(assignmentId, quizId, studentId, ranking, evaluate)
    } else {
      this.props.editAssignmentQuizMark(assignmentQuizMarkId, assignmentId, quizId, studentId, ranking, evaluate)
    }
  }

  isAddOrEditAction (assignmentQuizMarkId) {
    return assignmentQuizMarkId === -1
  }

  render () {
    const {assignmentQuizMark} = this.props
    return (
      <div>
        <div className='margin-t-3'>
          <h3><FormattedMessage id='作业等级' /></h3>
        </div>
        <SelectStatus
          value={assignmentQuizMark.ranking ? assignmentQuizMark.ranking : 'NoScore'}
          onChange={value => this.handleSubmit(value)}
          optionals={rankingStatus.rankingData}
        />
      </div>
    )
  }
}
const mapStateToProps = state => ({
  assignmentQuizMark: state.assignmentQuizMark
})
const mapDispatchToProps = dispatch => ({
  editAssignmentQuizMark: (id, assignmentId, quizId, studentId, ranking, evaluate) => dispatch(
        assignmentQuizMarkAction.editAssignmentQuizMark(id, assignmentId, quizId, studentId, ranking, evaluate)),
  addAssignmentQuizMark: (assignmentId, quizId, studentId, ranking, evaluate) => dispatch(
        assignmentQuizMarkAction.addAssignmentQuizMark(assignmentId, quizId, studentId, ranking, evaluate))
})

export default withRouter(connect(mapStateToProps, mapDispatchToProps)(AssignmentRankingBox))
